package com.zuowei.ycy.user.ui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RadioGroup;

import com.and.yzy.frame.config.HttpConfig;
import com.and.yzy.frame.update.AppUpdateUtil;
import com.and.yzy.frame.update.UpdateCallBack;
import com.and.yzy.frame.util.AppManger;
import com.zuowei.ycy.R;
import com.zuowei.ycy.ui.BaseAty;
import com.zuowei.ycy.ui.BaseFgt;
import com.zuowei.ycy.user.config.AppConfig;
import com.zuowei.ycy.user.config.UserManger;
import com.zuowei.ycy.user.ui.index.IndexFgt;
import com.zuowei.ycy.user.ui.login.LoginAty;
import com.zuowei.ycy.user.ui.mine.MineFgt;
import com.zuowei.ycy.user.ui.order.OrderFgt;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import okhttp3.FormBody;

/**
 * Created by yzy on 2016/4/18.
 */
public class MainAty extends BaseAty {
    @Bind(R.id.view_pager)
    ViewPager mViewPager;
    @Bind(R.id.rg_mian)
    RadioGroup mRgMian;

    private List<BaseFgt> mBaseFgts;

    private int checkid;
    private LogoutReceiver logoutReceiver;

    @Override
    public int getLayoutId() {
        return R.layout.main_activity;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (logoutReceiver!=null){
            unregisterReceiver(logoutReceiver);
        }
    }

    @Override
    public void initData() {
        logoutReceiver = new LogoutReceiver();
        IntentFilter  intentFilter = new IntentFilter(AppConfig.ACTION_LOGOUT);
        registerReceiver(logoutReceiver,intentFilter);

        FormBody formBody = new FormBody.Builder().add("type", "1").build();

        AppUpdateUtil updateUtil = new AppUpdateUtil(this, HttpConfig.UPDATE_URL, formBody);

        updateUtil.checkUpdate(new UpdateCallBack() {
            @Override
            public void onError() {

            }

            @Override
            public void isUpdate(String result) {

            }

            @Override
            public void isNoUpdate() {

            }
        });


        mBaseFgts = new ArrayList<>();
        mBaseFgts.add(new IndexFgt());
        mBaseFgts.add(new OrderFgt());
        mBaseFgts.add(new MineFgt());

        checkid = R.id.rb_fabu;
        MainFragmentAdapter mainFragmentAdapter = new MainFragmentAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mainFragmentAdapter);
        mViewPager.setOffscreenPageLimit(3);

        mRgMian.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_fabu:
                        mViewPager.setCurrentItem(0);
                        checkid = R.id.rb_fabu;
                        break;
                    case R.id.rb_dingdan:
                        if (UserManger.isLogin()) {
                            mViewPager.setCurrentItem(1);
                            checkid = R.id.rb_dingdan;
                        }
                        break;
                    case R.id.rb_wode:
                        if (UserManger.isLogin()) {
                            mViewPager.setCurrentItem(2);
                            checkid = R.id.rb_wode;
                        }
                        break;
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        //        if (!UserManger.isLogin()) {
        //            mRgMian.check(R.id.rb_fabu);
        //        }
        mRgMian.check(checkid);
    }

    @Override
    public void requestData() {

    }


    @OnClick({R.id.rb_wode, R.id.rb_dingdan})
    @Override
    public void btnClick(View view) {
        super.btnClick(view);
        switch (view.getId()) {
            case R.id.rb_wode:
            case R.id.rb_dingdan:
                if (!UserManger.isLogin()) {
                    startActivity(LoginAty.class, null);
                }
                break;
        }

    }

    class MainFragmentAdapter extends FragmentPagerAdapter {


        public MainFragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mBaseFgts.get(position);
        }

        @Override
        public int getCount() {
            return mBaseFgts.size();
        }
    }

    private long oldTime;

    @Override
    public void onBackPressed() {
        //        super.onBackPressed();
        long newtime = System.currentTimeMillis();
        if (newtime - oldTime > 3000) {
            oldTime = newtime;
            showToast("连按两次返回桌面");
        } else {
            //            AppManager.getInstance().killAllActivity();
            setHasAnimiation(false);
            AppManger.getInstance().AppExit(this);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        // super.onSaveInstanceState(outState);
    }

    /**
     * 退出登陆的广播
     */
    public class LogoutReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(AppConfig.ACTION_LOGOUT)){
                checkid = R.id.rb_fabu;
                mRgMian.check(R.id.rb_fabu);
            }
        }
    }

}
